package com.wenda.controller;


import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wenda.entity.Thesis;
import com.wenda.entity.User;
import com.wenda.service.ThesisService;
import com.wenda.service.UserService;

@Controller
public class ThesisController {
	@Autowired
	ThesisService thesisService;
	@Autowired
	UserService userService;
	
	@RequestMapping("/thesis")
	public String addThesis(Thesis thesis,HttpServletRequest request) {
		System.out.println(thesis);
		try {
			//1、进行论题校验
			int result = thesisService.checkThesis(thesis.getThesisName());
			//判断可创建的论题数量是否为0
			User user = (User)request.getSession().getAttribute("user");
			if(user.getThesisNumber() == 0) {
				request.setAttribute("msg", "用户论题数量已达最大值");
				return "user/submit";
			}
			if(result == -1) {
				request.setAttribute("msg", "论题重复");
				request.setAttribute("thesis", thesis);
				return "user/submit";
			} else if(result == 0){
				//2、补全论题信息
				//论题状态：-1：审核失败，0：等待初审，1：初审通过，2：审核通过
				thesis.setThesisStatus(0);
				//所属院系
				thesis.setCollegeId(user.getCollegeId());
				//创建时间
				thesis.setCreateDate(new Date());
				//创建人id
				//获取创建人信息
				thesis.setCreateId(user.getId());
				//修改创建者可拥有的论题数量
				user.setThesisNumber(user.getThesisNumber()-1);
				//3、保存论题信息
				thesisService.insert(thesis);
				userService.updateUser(user);
				request.setAttribute("msg", "提交成功，请等待管理员初审");
				return "user/submit";
			} else {
				//2、补全论题信息
				//论题状态：-1：审核失败，0：等待初审，1：初审通过，2：审核通过
				thesis.setThesisStatus(1);
				//所属院系
				thesis.setCollegeId(user.getCollegeId());
				//创建时间
				thesis.setCreateDate(new Date());
				//创建人id
				//获取创建人信息
				thesis.setCreateId(user.getId());
				//修改创建者可拥有的论题数量
				user.setThesisNumber(user.getThesisNumber()-1);
				//3、保存论题信息,同时更新用户可用论题数量
				thesisService.insert(thesis);
				userService.updateUser(user);
				request.setAttribute("msg", "提交成功，请等待复审");
				return "user/submit";
			}
			//根据校验结果处理数据
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("msg", "服务器错误");
			return "user/submit";
		}
	}
	@RequestMapping("/thesis/search/{id}")
	public String searchThesisByUser(@PathVariable("id") Integer id,HttpServletRequest request) {
		List<Thesis> result = thesisService.findByUserId(id);
		request.setAttribute("thesises", result);
		return "user/Queryindividual";
	}
	
	@RequestMapping("/thesis/check/{collegeId}")
	public String searchThesisByCollege(@PathVariable("collegeId") Integer collegeId,HttpServletRequest request) {
		//判断用户级别
		User user = (User)request.getSession().getAttribute("user");
		List<Thesis> result = null;
		if(user.getRoleId() == 4) {
			//查询未初审论题
			result = thesisService.findByCollegeWithStatus(collegeId,0);
		} else if(user.getRoleId() == 5) {
			//查询初审通过的论题
			result = thesisService.findByCollegeWithStatus(collegeId,1);
		}
		request.setAttribute("thesises", result);
		return "user/check_topic";
	}
	//论题状态：-1：审核失败，0：等待初审，1：初审通过，2：审核通过
	@RequestMapping("/thesis/{thesisId}/{thesisStatus}")
	public String updateThesisStatus(Thesis thesis,HttpServletRequest request) {
		//System.out.println(thesis);
		//更新论题
		thesisService.updateThesis(thesis);
		//数据回显
		User user = (User)request.getSession().getAttribute("user");
		return "redirect:/thesis/check/"+user.getCollegeId();
	}
	/**
	 * 初始化索引
	 */
	@RequestMapping(value="/thesis/init",method=RequestMethod.POST)
	@ResponseBody
	public String initThesisIndex() {
		return thesisService.initThesisIndex();
	}
	
	
}
